package com.ipworkbench.common.struts2.action;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * Action基类
 * 
 * @author zhangfaheng
 *
 * @date Oct 27, 2009
 */
@SuppressWarnings("serial")
public class BaseAction extends ActionSupport {
	
	public static final String LOGIN = "login";
	
	/**
	 * action返回到页面的消息字段
	 */
	protected String msg;
	//当前用户角色类型
	protected String roleType;
	 
	protected String jsonStr;
	protected List<Map<String,Object>> jsonList=new ArrayList<Map<String,Object>>();
	protected Map<String,List<Map<String, Object>>> jsonMapList=new HashMap<String,List<Map<String, Object>>>();
	protected Map<String, Object> jsonMap=new HashMap<String, Object>();
	
	
	//超级管理员状态
//	protected String superAdminStatus = SUPER_ADMIN_STATUS;
//	//管理员状态
//	protected String adminStatus = ADMIN_STATUS;
//	//用户状态
//	protected String empStatus = ROLE_STATUS;
	
	
	
	
	/**
	 * 读取表单参数
	 * 
	 * @param name
	 *            参数变量名
	 * @return 表单参数
	 */
	public String getParameter(String name) {

		return ServletActionContext.getRequest().getParameter(name);
	}
    
	public Map<String, List<Map<String, Object>>> getJsonMapList() {
		return jsonMapList;
	}

	public void setJsonMapList(Map<String, List<Map<String, Object>>> jsonMapList) {
		this.jsonMapList = jsonMapList;
	}

	public String getJsonStr() {
		return jsonStr;
	}

	public void setJsonStr(String jsonStr) {
		this.jsonStr = jsonStr;
	}

	public List<Map<String, Object>> getJsonList() {
		return jsonList;
	}

	public void setJsonList(List<Map<String, Object>> jsonList) {
		this.jsonList = jsonList;
	}

	public Map<String, Object> getJsonMap() {
		return jsonMap;
	}

	public void setJsonMap(Map<String, Object> jsonMap) {
		this.jsonMap = jsonMap;
	}

	/**
	 * 读取表单参数名
	 * 
	 * @return Enumeration<String>
	 */
	@SuppressWarnings("unchecked")
	public Enumeration<String> getParameterNames() {
		return ServletActionContext.getRequest().getParameterNames();
	}
	
	/**
	 * 设置request的属性
	 * 
	 * @param name
	 *            属性名
	 * @param value
	 *            属性值
	 */
	public void setAttribute(String name, Object value) {
		ServletActionContext.getRequest().setAttribute(name, value);
	}

	/**
	 * 获取request的属性
	 * 
	 * @param name
	 *            属性名
	 * @return 属性对象
	 */
	public Object getAttribute(String name) {
		return ServletActionContext.getRequest().getAttribute(name);
	}

	/**
	 * 获取session中的属性值
	 * 
	 * @param name
	 *            属性名
	 * @return 属性对象
	 */
	@SuppressWarnings("unchecked")
	public static Object getSession(String name) {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		return session.get(name);
	}
	
	/**
	 * 移除session中指定名称的值
	 * 
	 * @param name
	 *            属性名
	 * @return 属性对象
	 */
	public static Object removeSession(String name){
		return ActionContext.getContext().getSession().remove(name);
	} 
	/**
	 * 清空session
	 */
	public static void clearSession(){
		ActionContext.getContext().getSession().clear();
	}

	/**
	 * 向session设置属性值
	 * 
	 * @param name
	 *            属性名
	 * @param value
	 *            属性值
	 */
	@SuppressWarnings("unchecked")
	public static void setSession(Object name, Object value) {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		session.put(name, value);
	}

	/**
	 * 获取application对象
	 * 
	 * @return application对象
	 */
	public static ServletContext getApplication() {
		return ServletActionContext.getServletContext();
	}

	/**
	 * 获取请求对象
	 * 
	 * @return 请求对象
	 */
	public static HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}
	/**
	 * 获取响应对象
	 * 
	 * @return 响应对象
	 */
	public static HttpServletResponse getResponse(){
		return ServletActionContext.getResponse();
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getRoleType() {
		return roleType;
	}

	public void setRoleType(String roleType) {
		this.roleType = roleType;
	}

}
